package com.amorcloud.pig.utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.zebra.adc.decoder.Barcode2DWithSoft;

import java.io.UnsupportedEncodingException;

import HJ.EpcDecode.Decode;
import tool.EpcCrypto;

public class ScanQRCodeUtil {

    EditText editText;
    String barCode = "";
    String seldata = "ASCII";
    Barcode2DWithSoft barcode2DWithSoft = null;
    Context context;

    public ScanQRCodeUtil(View view, Context context) {
        this.context = context;
        barcode2DWithSoft = Barcode2DWithSoft.getInstance();
        editText = (EditText) view;
        new InitTask().execute();
    }

    public void ScanBarcode() {
        if (barcode2DWithSoft != null) {
            //Log.i(TAG,"ScanBarcode");
            barcode2DWithSoft.scan();
            barcode2DWithSoft.setScanCallback(ScanBack);
        }
    }

    public Barcode2DWithSoft.ScanCallback ScanBack = new Barcode2DWithSoft.ScanCallback() {
        @Override
        public void onScanComplete(int i, int length, byte[] bytes) {
            if (length < 1) {
                if (length == -1) {
                    editText.setText("Scan cancel");
                } else if (length == 0) {
                    editText.setText("Scan TimeOut");
                } else {
                    //Log.i(TAG,"Scan fail");
                }
            } else {
                //SoundManage.PlaySound(MainActivity.this, SoundManage.SoundType.SUCCESS);
                barCode = "";


                //  String res = new String(dd,"gb2312");
                try {
                    Log.i("leihuiming", "byte = " + bytes);

                    barCode = new String(bytes, 0, length, seldata);
                    Log.d("leihuiming", "barCode= " + barCode);
                    //barCode = Decode.GetDecodeEpc(barCode);
                    barCode = EpcCrypto.Decode(barCode);
                    Log.d("leihuiming", "barCode Decode = " + barCode);
                    //zt();
                } catch (UnsupportedEncodingException ex) {
                    ex.printStackTrace();
                }
                editText.setText(barCode);
            }

        }
    };


    public class InitTask extends AsyncTask<String, Integer, Boolean> {
        ProgressDialog mypDialog;

        @Override
        protected Boolean doInBackground(String... params) {
            // TODO Auto-generated method stub


            boolean reuslt = false;
            if (barcode2DWithSoft != null) {
                reuslt = barcode2DWithSoft.open(context);
                //Log.i(TAG,"open="+reuslt);
            }
            return reuslt;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            if (result) {
//                barcode2DWithSoft.setParameter(324, 1);
//                barcode2DWithSoft.setParameter(300, 0); // Snapshot Aiming
//                barcode2DWithSoft.setParameter(361, 0); // Image Capture Illumination

                // interleaved 2 of 5
                barcode2DWithSoft.setParameter(6, 1);
                barcode2DWithSoft.setParameter(22, 0);
                barcode2DWithSoft.setParameter(23, 55);
                barcode2DWithSoft.setParameter(402, 1);
                Toast.makeText(context, "二维码SDK初始化成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "二维码SDK初始化失败", Toast.LENGTH_SHORT).show();
            }
            mypDialog.cancel();
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

            mypDialog = new ProgressDialog(context);
            mypDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mypDialog.setMessage("init...");
            mypDialog.setCanceledOnTouchOutside(false);
            mypDialog.show();
        }

    }

    public void close() {
        if(barcode2DWithSoft != null) {
            barcode2DWithSoft.stopScan();
            barcode2DWithSoft.close();
        }
    }
}
